package com.cib.ajax.rpc.util;

import org.jabsorb.JSONSerializer;
import org.jabsorb.serializer.SerializerState;

import com.cib.ajax.rpc.serializer.BigIntegerSerializer;
import com.cib.ajax.rpc.serializer.EnumSerializer;

public class JavaBean2JSON {
    private JSONSerializer serializer;
    
    public JavaBean2JSON() {
        serializer = new JSONSerializer();
        try {
            serializer.registerDefaultSerializers();
            EnumSerializer enumSerializer = new EnumSerializer();          
            serializer.registerSerializer(enumSerializer);
            serializer.registerSerializer(new BigIntegerSerializer());
          

        } catch (Exception e) {
            //throw e;
        }
        serializer.setMarshallClassHints(false);
        serializer.setMarshallNullAttributes(true);
        serializer.setFixupDuplicates(false);

    }


    public Object toJSON(Object source) throws Exception {
        if (source == null) {
            return org.json.JSONObject.NULL;
        }

        SerializerState state = new SerializerState();
        return serializer.marshall(state,null,source,new Integer(0));
    }

}
